Object.defineProperty(exports, "__esModule", {
    value: true
});

var _util;

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function(obj) {
    return typeof obj;
} : function(obj) {
    return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};

var _md = require("./md5.js");

var _md2 = _interopRequireDefault(_md);

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}

function _defineProperty(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}

var formatTime = function formatTime(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return [ year, month, day ].map(formatNumber).join("/") + " " + [ hour, minute, second ].map(formatNumber).join(":");
};

var formatDate = function formatDate(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return [ year, month, day ].map(formatNumber).join("-");
};

var formatNumber = function formatNumber(n) {
    n = n.toString();
    return n[1] ? n : "0" + n;
};

var sign = function sign(obj) {
    if ((typeof obj === "undefined" ? "undefined" : _typeof(obj)) !== "object") {
        throw new TypeError("must be Object");
    }
    var keyArray = [];
    var parts = [];
    for (var key in obj) {
        keyArray.push(key);
    }
    keyArray.sort();
    keyArray.forEach(function(item) {
        parts.push(item + "=" + obj[item]);
    });
    var hashStrFirst = (0, _md2.default)("/reg_code/query" + "123" + parts.join("&"));
    // console.log("sms/send" + "123" + parts.join("&"));
    // console.log(hashStrFirst);
    //将secret 混淆到结果中
    //hashStrFirst.splice(7, 0, "123");
        var hashStrFirstArray = hashStrFirst.split("");
    hashStrFirstArray.splice(7, 0, "123");
    //console.log(hashStrFirstArray.join(""));
        return (0, _md2.default)(hashStrFirstArray.join(""));
};

var school_sign = function school_sign(obj) {
    if ((typeof obj === "undefined" ? "undefined" : _typeof(obj)) !== "object") {
        throw new TypeError("must be Object");
    }
    var keyArray = [];
    var parts = [];
    for (var key in obj) {
        keyArray.push(key);
    }
    keyArray.sort();
    keyArray.forEach(function(item) {
        parts.push(item + "=" + obj[item]);
    });
    var hashStrFirst = (0, _md2.default)("/smsCode" + "123" + parts.join("&"));
    var hashStrFirstArray = hashStrFirst.split("");
    hashStrFirstArray.splice(7, 0, "123");
    //console.log(hashStrFirstArray.join(""));
        return (0, _md2.default)(hashStrFirstArray.join(""));
};

var util = (_util = {
    formatTime: formatTime,
    formatDate: formatDate,
    formatTimeHHMM: function formatTimeHHMM(date) {
        var hour = date.getHours();
        var minute = date.getMinutes();
        return [ hour, minute ].map(formatNumber).join(":");
    },
    //验证手机号
    checkPhoneNum: function checkPhoneNum(phoneNum) {
        return /^1[0-9]{10}$/.test(phoneNum);
    },
    checkRegCode: function checkRegCode(regCode) {
        return /^[0-9]{4,6}$/.test(regCode);
    },
    sign: sign,
    getQueryStringArgs: function getQueryStringArgs(url) {
        var urlStrings = url.split("?");
        var qs = urlStrings[1] || "";
        var args = {}, items = qs.length ? qs.split("&") : [], item = null, name = null, value = null, i = 0, len = items.length;
        for (i = 0; i < len; i++) {
            item = items[i].split("=");
            name = item[0];
            value = item[1];
            if (name.length) {
                args[name] = value;
            }
        }
        return args;
    },
    computeCenter: function computeCenter(points) {
        var superPoint = points.reduce(function(_superPoint, currentPoint) {
            return {
                longitude: +_superPoint.longitude + +currentPoint.longitude,
                latitude: +_superPoint.latitude + +currentPoint.latitude
            };
        });
        return {
            longitude: superPoint.longitude / points.length,
            latitude: superPoint.latitude / points.length
        };
    },
    debounce: function debounce(func, wait, immediate) {
        var timeout, args, context, timestamp, result;
        var later = function later() {
            var last = +new Date() - timestamp;
            if (last < wait && last >= 0) {
                timeout = setTimeout(later, wait - last);
            } else {
                timeout = null;
                if (!immediate) {
                    result = func.apply(context, args);
                    if (!timeout) context = args = null;
                }
            }
        };
        return function() {
            context = this;
            args = arguments;
            timestamp = +new Date();
            var callNow = immediate && !timeout;
            if (!timeout) timeout = setTimeout(later, wait);
            if (callNow) {
                result = func.apply(context, args);
                context = args = null;
            }
            return result;
        };
    },
    createNonceStr: function createNonceStr() {
        return Math.random().toString(36).substr(2, 15);
    },
    after: function after(times, func) {
        return function() {
            if (--times < 1) {
                return func.apply(this, arguments);
            }
        };
    },
    formatAngle: function formatAngle(angle) {
        var _angle = angle;
        if (_angle < 0) {
            _angle = _angle + 360;
        }
        return _angle;
    },
    dateStringFormat: function dateStringFormat(dateString) {
        if (typeof dateString === "string") {
            return dateString.replace(/\-/g, "/");
        } else {
            return dateString;
        }
    },
    randomNum: function randomNum(minNum, maxNum) {
        return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum);
    },
    myFormatTime: function myFormatTime(time) {
        if (typeof time !== "number" || time < 0) {
            return time;
        }
        // var hour = parseInt(time / 3600);
        // time = time % 3600;
                var minute = parseInt(time / 60);
        time = time % 60;
        var second = time;
        return [ minute, second ].map(function(n) {
            n = n.toString();
            return n[1] ? n : "0" + n;
        }).join(":");
    },
    myFormatTimeDot: function myFormatTimeDot(time) {
        if (typeof time !== "number" || time < 0) {
            return time;
        }
        // var hour = parseInt(time / 3600);
        // time = time % 3600;
                var minute = parseInt(time / 60);
        time = time % 60;
        var second = time;
        return [ minute, second ].map(function(n) {
            n = n.toString();
            return n[1] ? n : "0" + n;
        }).join("'") + "''";
    },
    myFormatLocalTime: function myFormatLocalTime(time) {
        if (typeof time !== "number" || time < 0) {
            return time;
        }
        // var hour = parseInt(time / 3600);
        // time = time % 3600;
                var minute = parseInt(time / 60);
        time = time % 60;
        var second = time;
        return [ minute, second ].map(function(n) {
            n = n.toString();
            return n[1] ? n : "0" + n;
        }).join("分") + "秒";
    }
}, _defineProperty(_util, "computeCenter", function computeCenter(points) {
    var superPoint = points.reduce(function(_superPoint, currentPoint) {
        return {
            longitude: +_superPoint.longitude + +currentPoint.longitude,
            latitude: +_superPoint.latitude + +currentPoint.latitude
        };
    });
    return {
        longitude: superPoint.longitude / points.length,
        latitude: superPoint.latitude / points.length
    };
}), _defineProperty(_util, "includePointsTrim", function includePointsTrim(pointArray) {
    if (pointArray.length <= 1) {
        return pointArray;
    }
    var point1 = pointArray[0];
    var point2 = pointArray[1];
    var _point1 = {
        lng: Math.min(point1.longitude, point2.longitude),
        lat: Math.min(point1.latitude, point2.latitude)
    };
    var _point2 = {
        lng: Math.max(point1.longitude, point2.longitude),
        lat: Math.max(point1.latitude, point2.latitude)
    };
    var deltaLng = _point2.lng - _point1.lng;
    var deltaLat = _point2.lat - _point1.lat;
    var _pointArray = [ {
        longitude: _point1.lng - deltaLng / 5,
        latitude: _point1.lat - deltaLat / 5
    }, {
        longitude: _point2.lng + deltaLng / 5,
        latitude: _point2.lat + deltaLat / 5
    } ];
    return _pointArray;
}), _defineProperty(_util, "serialize", function serialize(obj) {
    var parts = [];
    for (var i in obj) {
        parts.push(encodeURIComponent(i) + "=" + encodeURIComponent(obj[i]));
    }
    return parts.join("&");
}), _defineProperty(_util, "formatFloat", function formatFloat(f, digit) {
    digit = digit != null ? digit : 2;
    var m = Math.pow(10, digit);
    return (Math.round(f * m, 10) / m).toFixed(digit);
}), _defineProperty(_util, "cmp", function cmp(x, y) {
    // If both x and y are null or undefined and exactly the same 
    if (x === y) {
        return true;
    }
    // If they are not strictly equal, they both need to be Objects 
        if (!(x instanceof Object) || !(y instanceof Object)) {
        return false;
    }
    //They must have the exact same prototype chain,the closest we can do is
    //test the constructor. 
        if (x.constructor !== y.constructor) {
        return false;
    }
    for (var p in x) {
        //Inherited properties were tested using x.constructor === y.constructor
        if (x.hasOwnProperty(p)) {
            // Allows comparing x[ p ] and y[ p ] when set to undefined 
            if (!y.hasOwnProperty(p)) {
                return false;
            }
            // If they have the same strict value or identity then they are equal 
                        if (x[p] === y[p]) {
                continue;
            }
            // Numbers, Strings, Functions, Booleans must be strictly equal 
                        if (_typeof(x[p]) !== "object") {
                return false;
            }
            // // Objects and Arrays must be tested recursively 
            // if (!this.cmp(x[p], y[p])) {
            //   return false;
            // }
                }
    }
    for (p in y) {
        // allows x[ p ] to be set to undefined 
        if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {
            return false;
        }
    }
    return true;
}), _defineProperty(_util, "getByteLen", function getByteLen(val) {
    var len = 0;
    if(val){
    for (var i = 0; i < val.length; i++) {
        var a = val.charAt(i);
        if (a.match(/[^\x00-\xff]/gi) != null) {
            len += 2;
        } else {
            len += 1;
        }
    }
}
    return len;
}), _defineProperty(_util, "deepCloneByJSON", function deepCloneByJSON(obj) {
    return JSON.parse(JSON.stringify(obj));
}), _defineProperty(_util, "getSameStr", function getSameStr(str1, str2) {
    var textArray = [];
    var max = str1.length > str2.length ? str1 : str2;
    var min = max == str1 ? str2 : str1;
    for (var i = 0; i < min.length; i++) {
        for (var x = 0, y = min.length - i; y != min.length + 1; x++, y++) {
            //y表示所取字符串的长度 
            var newStr = min.substring(x, y).replace(/[\[\]\(\)]/g, "");
            //判断max中是否包含newStr
                        if (max.toLowerCase().indexOf(newStr.toLowerCase()) >= 0) {
                textArray.push(newStr);
            }
        }
    }
    return textArray;
}), _defineProperty(_util, "school_sign", school_sign), _util);

exports.default = util;